home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.09 Sep 91 / Bug Parser Code / MiniEdit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-09  |  7.5 KB  |  338 lines  |  [TEXT/KAHL]

  1. /*********************************************************************
  2.  
  3.     Buggy MiniEdit.c
  4.     
  5.     The sample application from Inside Macintosh (RoadMap p.15-17)
  6.     beefed up a bit by Stephen Z. Stein, Symantec Corp.
  7.     Use this file with the “MiniEdit” chapter of your manual.
  8.     
  9.     The resources used in this program are in the file MiniEdit.π.rsrc.
  10.     The file was created with ResEdit, so there is no RMaker source
  11.     for it.
  12.     
  13.     In order for THINK C to find the resource file for this
  14.     project, be sure you’ve named the project MiniEdit.π
  15.     
  16.     *** There is a bug in this file! ***
  17.     
  18. *********************************************************************/
  19.  
  20. #include <QuickDraw.h>
  21. #include <MacTypes.h>
  22. #include <FontMgr.h>
  23. #include <WindowMgr.h>
  24. #include <MenuMgr.h>
  25. #include <TextEdit.h>
  26. #include <DialogMgr.h>
  27. #include <EventMgr.h>
  28. #include <DeskMgr.h>
  29. #include <FileMgr.h>
  30. #include <ToolboxUtil.h>
  31. #include <ControlMgr.h>
  32.  
  33. #include "MiniEdit.h"
  34.  
  35. WindowRecord    wRecord;
  36. WindowPtr        myWindow;
  37. TEHandle        TEH;
  38. int                linesInFolder;
  39. Rect            dragRect = { 0, 0, 1024, 1024 };
  40. MenuHandle        myMenus[3];
  41. ControlHandle     vScroll;
  42. Cursor            editCursor;
  43. Cursor            waitCursor;
  44. char            dirty;
  45.  
  46. extern Str255     theFileName;
  47.  
  48. main() 
  49. {
  50.     int        myRsrc;
  51.     
  52.     InitGraf(&thePort);
  53.     InitFonts();
  54.     FlushEvents( everyEvent, 0 );
  55.     InitWindows();
  56.     InitMenus();
  57.     TEInit();
  58.     InitDialogs(0L);
  59.     InitCursor();
  60.     MaxApplZone();
  61.  
  62. /*
  63. /*  The following statement is included as a check to see if we can
  64. /*    access our program's resources.  When the project is run from
  65. /*    THINK C, the resource file <project name>.rsrc is automatically
  66. /*  opened.  When an application is built, these resources are 
  67. /*     automatically merged with the application.
  68. /*
  69. */
  70.     
  71.     if (GetResource('MENU', fileID)==0) {
  72.         SysBeep(20);
  73.         CantOpen();
  74.         return;
  75.     }
  76.     
  77.     SetUpFiles();
  78.     SetUpCursors();
  79.     SetUpMenus();
  80.     SetUpWindows();
  81.     while (MainEvent()) ;
  82. }
  83.  
  84. int MainEvent() 
  85. {
  86. EventRecord        myEvent;
  87. WindowPtr        whichWindow;
  88. Rect            r;
  89. char            str[255];
  90.     
  91.     MaintainCursor();
  92.     MaintainMenus();
  93.     SystemTask();
  94.     TEIdle(TEH);
  95.     if (GetNextEvent(everyEvent, &myEvent)) {
  96.         switch (myEvent.what) {
  97.         case mouseDown:
  98.             ShowStrPnt(BP_CONTINUE,'EVNT',"\PmouseDown event",
  99.                 myEvent.where );
  100.             switch (FindWindow( myEvent.where, &whichWindow )) {
  101.             case inDesk: 
  102.                 SysBeep(10);
  103.                 break;
  104.             case inGoAway:
  105.                 if (ours(whichWindow))
  106.                     if (TrackGoAway( myWindow, myEvent.where) )
  107.                         DoFile(fmClose);
  108.                 break;
  109.             case inMenuBar:
  110.                 return( DoCommand( MenuSelect(myEvent.where) ) );
  111.             case inSysWindow:
  112.                 SystemClick( &myEvent, whichWindow );
  113.                 break;
  114.             case inDrag:
  115.                 if (ours(whichWindow))
  116.                     DragWindow( whichWindow, myEvent.where, &dragRect );
  117.                 break;
  118.             case inGrow:
  119.                 if (ours(whichWindow))
  120.                     MyGrowWindow( whichWindow, myEvent.where );
  121.                 break;
  122.             case inContent:
  123.                 if (whichWindow != FrontWindow())
  124.                     SelectWindow(whichWindow);
  125.                 else 
  126.                     if (ours(whichWindow))
  127.                         DoContent(whichWindow, &myEvent);
  128.                 break;
  129.             default: ;
  130.             } /* end switch FindWindow */
  131.             break;
  132.         case keyDown:
  133.         case autoKey: 
  134.             {
  135.             register char    theChar;
  136.             
  137.             theChar = myEvent.message & charCodeMask;
  138.             if ((myEvent.modifiers & cmdKey) != 0) 
  139.                 return( DoCommand( MenuKey( theChar ) ));
  140.             else {
  141.                 TEKey( theChar, TEH );
  142.                 ShowSelect();
  143.                 dirty = 1;
  144.                 }
  145.             }
  146.             break;
  147.         case activateEvt:
  148.             if (ours((WindowPtr)myEvent.message)) {
  149.                 r=(*myWindow).portRect;
  150.                 r.top = r.bottom - (SBarWidth+1);
  151.                 r.left = r.left - (SBarWidth+1);
  152.                 InvalRect(&r);
  153.                 if ( myEvent.modifiers & activeFlag ) {
  154.                     ShowPro(NULL,'WIND',"\PActivating window" );
  155.                     TEActivate( TEH );
  156.                     ShowControl( vScroll );
  157.                     DisableItem( myMenus[editM], undoCommand );
  158.                     TEFromScrap();
  159.                 }
  160.                 else {
  161.                     ShowPro(NULL,'WIND',"\PDeactivating window" );
  162.                     TEDeactivate(TEH);
  163.                     HideControl( vScroll );
  164.                     ZeroScrap();
  165.                     TEToScrap();
  166.                 }
  167.             }
  168.             break;
  169.         case updateEvt:
  170.             ShowStrL( NULL,'EVNT',"\PupdateEvt ->",
  171.                 (long )myEvent.message );
  172.             if (ours((WindowPtr)myEvent.message)) UpdateWindow(myWindow);
  173.             break;
  174.         default:
  175.             ShowStrL( NULL,'EVNT',"\Pdefault event ->",
  176.                 (long )myEvent.what );
  177.             break;
  178.         } /* end of case myEvent.what */
  179.     } /* if */
  180.     ShowStrL(BP_CONTINUE,'EVNT',"\P--->Event.what->",
  181.         (long )myEvent.what );
  182.     return(1);
  183. }
  184.  
  185. SetUpMenus()
  186. {
  187.     int        i;
  188.     
  189.     myMenus[appleM] = NewMenu( appleID, "\p\024" );
  190.     AddResMenu( myMenus[appleM], 'DRVR' );
  191.     myMenus[fileM] = GetMenu(fileID);
  192.     myMenus[editM] = GetMenu(editID);
  193.     for ( (i=appleM); (i<=editM); i++ ) InsertMenu(myMenus[i], 0) ;
  194.     DrawMenuBar();
  195.     Init_BP();            /* added this to install debugger */
  196. }
  197.  
  198. int DoCommand( mResult )
  199. long mResult;
  200. {
  201.     int        theItem, temp;
  202.     Str255    name;
  203.     WindowPeek wPtr;
  204.     
  205.     theItem = LoWord( mResult );
  206.     switch (HiWord(mResult)) {
  207.     case appleID:
  208.         GetItem(myMenus[appleM], theItem, &name);
  209.         OpenDeskAcc( &name );
  210.         SetPort( myWindow );
  211.         break;
  212.     case fileID: 
  213.         DoFile(theItem);
  214.         break;
  215.     case editID: 
  216.         if (SystemEdit(theItem-1)==0) {
  217.             wPtr = (WindowPeek) FrontWindow();
  218.             switch (theItem) {
  219.             case cutCommand:
  220.                 TECut( TEH );
  221.                 dirty = 1;
  222.                 break;
  223.             case copyCommand:
  224.                 TECopy( TEH );
  225.                 break;
  226.             case pasteCommand:
  227.                 TEPaste( TEH );
  228.                 dirty = 1;
  229.                 break;
  230.             case clearCommand:
  231.                 TEDelete( TEH );
  232.                 dirty = 1;
  233.                 break;
  234.             default: ;
  235.             }
  236.             ShowSelect();
  237.         }
  238.         break;
  239.     case BP_MENU_ID:
  240.         BPMenuChooser( theItem );
  241.         break;
  242.     }
  243.     HiliteMenu(0);
  244.     return(1);
  245. }
  246.  
  247. MaintainCursor()
  248. {
  249.     Point        pt;
  250.     WindowPeek    wPtr;
  251.     GrafPtr        savePort;
  252.     
  253.     if (ours((WindowPtr)(wPtr=(WindowPeek)FrontWindow()))) {
  254.         GetPort( &savePort );
  255.         SetPort( (GrafPtr)wPtr );
  256.         GetMouse(&pt);
  257.         if ( PtInRect(pt, &(**TEH).viewRect )  )
  258.             SetCursor( &editCursor);
  259.         else SetCursor( &arrow );
  260.         SetPort( savePort );
  261.     }
  262. }
  263.  
  264. MaintainMenus()
  265. {
  266.     if ( !(*(WindowPeek)myWindow).visible || 
  267.             !ours(FrontWindow()) ) {
  268.         EnableItem( myMenus[fileM], fmNew );
  269.         EnableItem( myMenus[fileM], fmOpen );
  270.         DisableItem( myMenus[fileM], fmClose );
  271.         DisableItem( myMenus[fileM], fmSave );
  272.         DisableItem( myMenus[fileM], fmSaveAs );
  273.         DisableItem( myMenus[fileM], fmRevert );
  274.         DisableItem( myMenus[fileM], fmPrint );
  275.         EnableItem( myMenus[editM], undoCommand );
  276.         EnableItem( myMenus[editM], cutCommand );
  277.         EnableItem( myMenus[editM], copyCommand );
  278.         EnableItem( myMenus[editM], clearCommand );
  279.     }
  280.     else {
  281.         DisableItem( myMenus[fileM], fmNew );
  282.         DisableItem( myMenus[fileM], fmOpen );
  283.         EnableItem( myMenus[fileM], fmClose );
  284.         EnableItem( myMenus[fileM], fmSaveAs );
  285.         EnableItem( myMenus[fileM], fmPrint );
  286.         if (dirty && theFileName[0] != 0) {
  287.             EnableItem( myMenus[fileM], fmRevert );
  288.             EnableItem( myMenus[fileM], fmSave );
  289.         }
  290.         else {
  291.             DisableItem( myMenus[fileM], fmRevert );
  292.             DisableItem( myMenus[fileM], fmSave );
  293.         }
  294.         DisableItem( myMenus[editM], undoCommand );
  295.         if ((**TEH).selStart==(**TEH).selEnd) {
  296.             DisableItem( myMenus[editM], cutCommand );
  297.             DisableItem( myMenus[editM], copyCommand );
  298.             DisableItem( myMenus[editM], clearCommand );
  299.         }
  300.         else {
  301.             EnableItem( myMenus[editM], cutCommand );
  302.             EnableItem( myMenus[editM], copyCommand );
  303.             EnableItem( myMenus[editM], clearCommand );
  304.         }
  305.     }
  306. }
  307.  
  308. SetUpCursors()
  309. {
  310.     CursHandle    hCurs;
  311.     
  312.     hCurs = GetCursor(1);
  313.     editCursor = **hCurs;
  314.     hCurs = GetCursor(watchCursor);
  315.     waitCursor = **hCurs;
  316. }
  317.  
  318. ours(w)
  319. WindowPtr w;
  320. {
  321.     return( (myWindow!=NULL) && (w==myWindow) );
  322. }
  323.  
  324. CantOpen()
  325. {
  326.     Rect r;
  327.  
  328.     SetRect(&r, 152, 60, 356, 132);
  329.     SetPort((myWindow = NewWindow( 0L, &r, "\p", 1, 1, -1L, 0, 0L)));
  330.     TextFont(0);
  331.     MoveTo( 4, 20 );
  332.     DrawString("\pCan't open resource file.");
  333.     MoveTo( 4, 40 );
  334.     DrawString("\pClick mouse to exit.");
  335.     do {
  336.     } while ( !Button() );
  337. }
  338.